package com.info.zhiduoduo.repository.mapper.partner;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.partner.PartnerMonthlyCostDto;
import com.info.zhiduoduo.repository.dto.partner.PartnerMonthlyCrowdsourceCostDto;
import com.info.zhiduoduo.repository.entity.partner.PartnerMonthlyCrowdCost;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author bob
 * @since 2021-01-24
 */
public interface PartnerMonthlyCrowdCostMapper extends BaseMapper<PartnerMonthlyCrowdCost> {

	@Select(" select pmcc.*,pa.realname as partnerName " +
			" from zdd_partner_monthly_crowd_cost pmcc " +
			" join zdd_partner_account pa on pa.partnerid = pmcc.partnerid and pa.realname like '%${name}%' " +
			" where pmcc.total_profit != 0 and IF(#{partnerid}=0, 1=1, pmcc.partnerid=#{partnerid})" +
			" and IF(#{state}=0, 1=1, pmcc.state=#{state}) " +
			" and IF(ISNULL(#{from}), 1=1, pmcc.month >= date_format(#{from}, '%Y-%m-01')) " +
			" and IF(ISNULL(#{to}), 1=1, pmcc.month <= date_format(#{to}, '%Y-%m-01')) ")
	List<PartnerMonthlyCrowdsourceCostDto> list(int partnerid, String name, int state, Date from, Date to);


	// 查询 合伙人众包成本佣金记录 列表
	@Select(" select pmcc.month,pmcc.partnerid,pmcc.id,pmcc.own_profit,pmcc.sub_profit,pmcc.total_profit,pmcc.state,pmcc.createtime " +
			" from zdd_partner_monthly_crowd_cost pmcc  " +
			" where pmcc.total_profit > 0 " +
			" and pmcc.partnerid = #{partnerid} " +
			" and IF(#{state}=0, 1=1, pmcc.state=#{state}) " +
			" order by pmcc.createtime desc ")
	List<PartnerMonthlyCostDto> listMonthlyCost(@Param("partnerid")int partnerid , @Param("state")int state);

	@Select(" select IFNULL(SUM(total_profit), 0) " +
			" from zdd_partner_monthly_crowd_cost pmcc  " +
			" where pmcc.partnerid = #{partnerid} " +
			" and IF(#{state}=0, 1=1, pmcc.state=#{state}) ")
	BigDecimal sumPartnerBrokerage(@Param("partnerid") int partnerid, @Param("state") int state);

	@Select(" select IFNULL(SUM(total_profit), 0) from zdd_partner_monthly_crowd_cost pmcc where pmcc.state=#{state} ")
	BigDecimal getUnCostCommissionAmount(int state);

}
